home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Oh!X 2001 Spring
/
Oh!X 2001 Spring Special CD-ROM (Japan).7z
/
Oh!X 2001 Spring Special CD-ROM (Japan) (Track 1).bin
/
JAVA
/
jshooting3
/
jshooting.java
< prev
next >
Wrap
Text File
|
2000-08-18
|
9KB
|
338 lines
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.lang.Math;
import java.util.Random;
public class jshooting extends Applet implements KeyListener, ActionListener, Runnable {
static final int WIDTH = 240, HEIGHT = 320; // 背景サイズ
static final int SHOTNUM = 5; // 弾の数
static final int ENEMYNUM = 10; // 敵の数
static final int BULLETNUM = 20; // 敵弾の数
static final int SHOT = 1; // 弾のプレーン番号
static final int ENEMY = SHOT+SHOTNUM; // 敵のプレーン番号
static final int BULLET = ENEMY+ENEMYNUM; // 敵弾のプレーン番号
SpriteCanvas sc; // スプライトキャンバス
MediaTracker mt;
boolean up=false, down=false, left=false, right=false, zkey=false;
int mx, my;
int zkeyblank;
Thread thread=null;
Label Score;
Button StartBtn;
int score=0;
int bgwidth[] = { WIDTH };
int bgheight[] = { HEIGHT };
int bgnum[] = { 1 };
int scroll = 0;
boolean shot[] = new boolean[SHOTNUM];
int shot_x[] = new int[SHOTNUM], shot_y[] = new int[SHOTNUM];
boolean enemy[] = new boolean[ENEMYNUM];
int enemy_x[] = new int[ENEMYNUM], enemy_y[] = new int[ENEMYNUM];
boolean bullet[] = new boolean[BULLETNUM];
int bullet_x[] = new int[BULLETNUM], bullet_y[] = new int[BULLETNUM];
int bullet_dx[] = new int[BULLETNUM], bullet_dy[] = new int[BULLETNUM];
boolean play = false;
Random aRandom = new Random();
Math aMath;
public void init(){
// パターン 背景1、自機1、弾1、敵1、敵弾1、爆発1、合計6
// プレーン 自機1、弾5、敵10、敵弾20、合計36
sc = new SpriteCanvas( 6, 36, WIDTH, HEIGHT, this );
setLayout( new BorderLayout() );
Score = new Label( "SCORE "+score, Label.CENTER );
StartBtn = new Button( "Start" );
add( "Center", sc );
add( "North", Score );
add( "South", StartBtn );
mt = new MediaTracker( this );
Image image = getImage( getDocumentBase(), "img/back.gif" ); // 背景
mt.addImage( image, 0 );
sc.Define( 0, image );
// バックグラウンド1プレーン
sc.CreateBackground( 1, bgwidth, bgheight, bgnum );
sc.SetBGPattern( 0, 0, 0, 0, 0 );
sc.BGShow();
image = getImage( getDocumentBase(), "img/myship.gif" ); // 自機
mt.addImage( image, 0 );
sc.Define( 1, image );
image = getImage( getDocumentBase(), "img/shot.gif" ); // 弾
mt.addImage( image, 0 );
sc.Define( 2, image );
image = getImage( getDocumentBase(), "img/enemy.gif" ); // 敵
mt.addImage( image, 0 );
sc.Define( 3, image );
image = getImage( getDocumentBase(), "img/bullet.gif" ); // 敵弾
mt.addImage( image, 0 );
sc.Define( 4, image );
image = getImage( getDocumentBase(), "img/bomb.gif" ); // 爆発
mt.addImage( image, 0 );
sc.Define( 5, image );
sc.WaitLoadImage( mt, 0 );
StartBtn.addKeyListener( this );
StartBtn.addActionListener( this );
}
public void GameStart(){
int i;
// 自機のセット
sc.Set( 0, 1 );
mx = (WIDTH-32)/2;
my = HEIGHT-32*2;
sc.Move( 0, mx, my );
play = true;
score = 0;
sc.Hide();
for( i=0; i<5; i++ ) shot[i] = false;
for( i=0; i<10; i++ ) enemy[i] = false;
for( i=0; i<20; i++ ) bullet[i] = false;
Score.setText( "SCORE "+score );
sc.Show( 0, 0 );
}
public void keyPressed( KeyEvent e ){
switch( e.getKeyCode() ){
case KeyEvent.VK_UP:
up = true;
break;
case KeyEvent.VK_DOWN:
down = true;
break;
case KeyEvent.VK_LEFT:
left = true;
break;
case KeyEvent.VK_RIGHT:
right = true;
break;
case KeyEvent.VK_Z:
if( zkey==false ){
zkey = true;
zkeyblank = 0;
}
break;
}
}
public void keyReleased( KeyEvent e ){
switch( e.getKeyCode() ){
case KeyEvent.VK_UP:
up = false;
break;
case KeyEvent.VK_DOWN:
down = false;
break;
case KeyEvent.VK_LEFT:
left = false;
break;
case KeyEvent.VK_RIGHT:
right = false;
break;
case KeyEvent.VK_Z:
zkey = false;
break;
}
}
public void keyTyped( KeyEvent e ){
}
public void actionPerformed( ActionEvent e ){
if( e.getActionCommand().equalsIgnoreCase( "Start" ) ){
if( play==false ){ // ゲームスタート
GameStart();
}
}
}
public void start(){
if( thread==null ){
thread = new Thread( this );
thread.start();
}
}
public void stop(){
if( thread!=null ){
thread = null;
}
}
public void run(){
int i, j;
try {
mt.waitForID( 0 );
} catch( InterruptedException e ){
return;
}
while( thread!=null ){
try {
Thread.sleep( 50 );
} catch( InterruptedException e ){
break;
}
if( play ){
if( up ){
my -= 4;
if( my<0 ) my = 0;
}
if( down ){
my += 4;
if( my>HEIGHT-32 ) my = HEIGHT-32;
}
if( left ){
mx -= 4;
if( mx<0 ) mx = 0;
}
if( right ){
mx += 4;
if( mx>WIDTH-32 ) mx = WIDTH-32;
}
sc.Move( 0, mx, my );
// 弾の発射
if( zkey ){
// 押しっぱなしのブランク期間
if( zkeyblank==0 ){
// 空きを検索
for( i=0; i<SHOTNUM; i++ ){
if( shot[i]==false ) break;
}
// 弾の設定
if( i<SHOTNUM ){
shot_x[i] = mx;
shot_y[i] = my;
sc.Set( i+SHOT, 2 );
sc.Show( i+SHOT, i+SHOT );
shot[i] = true;
}
}
zkeyblank = (zkeyblank+1)%4;
}
// 敵の出現
if( (aRandom.nextInt()&0xf)==0 ){
// 空きを検索
for( i=0; i<ENEMYNUM; i++ ){
if( enemy[i]==false ) break;
}
// 敵の設定
if( i<ENEMYNUM ){
enemy_x[i] = Math.abs(aRandom.nextInt())%(WIDTH-32);
enemy_y[i] = -32;
sc.Set( i+ENEMY, 3 );
sc.Show( i+ENEMY, i+ENEMY );
enemy[i] = true;
}
}
// 敵の移動
for( i=0; i<ENEMYNUM; i++ ){
// 表示されている敵の検索
if( enemy[i] ){
// 移動
enemy_y[i] += 4;
// 画面外判定
if( enemy_y[i]>=HEIGHT ){
sc.Hide( i+ENEMY, i+ENEMY );
enemy[i] = false;
} else {
sc.Move( i+ENEMY, enemy_x[i], enemy_y[i] );
// 敵弾発射
if( (aRandom.nextInt()&0xf)==0 ){
// 空きを検索
for( j=0; j<BULLETNUM; j++ ){
if( bullet[j]==false ) break;
}
// 敵弾の設定
if( j<BULLETNUM ){
// 背後からの攻撃は禁止
if( my>enemy_y[i] ){
double l = aMath.sqrt((mx-enemy_x[i])*(mx-enemy_x[i])+(my-enemy_y[i])*(my-enemy_y[i]));
// 至近距離からの発射は禁止
if( l>100.0 ){
bullet_x[j] = enemy_x[i];
bullet_y[j] = enemy_y[i];
bullet_dx[j] = (int)aMath.round( 6*(mx-enemy_x[i])/l );
bullet_dy[j] = (int)aMath.round( 6*(my-enemy_y[i])/l );
sc.Set( j+BULLET, 4 );
sc.Show( j+BULLET, j+BULLET );
bullet[j] = true;
}
}
}
}
// 自機との当り判定
if( enemy_x[i]>mx-18 && enemy_x[i]<mx+18 ){
if( enemy_y[i]>my-18 && enemy_y[i]<my+18 ){
// 敵爆発
sc.Set( i+ENEMY, 5 );
// 自機爆発
sc.Set( 0, 5 );
enemy[i] = false;
score += 10;
Score.setText( "SCORE "+score );
play = false;
}
}
}
} else {
sc.Hide( i+ENEMY, i+ENEMY );
}
}
// 弾の移動
for( i=0; i<SHOTNUM; i++ ){
// 表示されている弾の検索
if( shot[i] ){
// 移動
shot_y[i] -= 16;
// 画面外判定
if( shot_y[i]<=-32 ){
sc.Hide( i+SHOT, i+SHOT );
shot[i] = false;
} else {
sc.Move( i+SHOT, shot_x[i], shot_y[i] );
// 敵との当り判定
for( j=0; j<ENEMYNUM; j++ ){
if( enemy[j] ){
if( shot_x[i]>enemy_x[j]-12 && shot_x[i]<enemy_x[j]+12 ){
if( shot_y[i]>enemy_y[j]-12 && shot_y[i]<enemy_y[j]+12 ){
// 弾消去
sc.Hide( i+SHOT, i+SHOT );
shot[i] = false;
// 敵爆発
sc.Set( j+ENEMY, 5 );
enemy[j] = false;
score += 10;
Score.setText( "SCORE "+score );
}
}
}
}
}
}
}
// 敵弾の移動
for( i=0; i<BULLETNUM; i++ ){
// 表示されている敵弾の検索
if( bullet[i] ){
// 移動
bullet_x[i] += bullet_dx[i];
bullet_y[i] += bullet_dy[i];
// 画面外判定
if( bullet_x[i]<=-32 || bullet_x[i]>=WIDTH ||
bullet_y[i]<=-32 || bullet_y[i]>=HEIGHT ){
sc.Hide( i+BULLET, i+BULLET );
bullet[i] = false;
} else {
sc.Move( i+BULLET, bullet_x[i], bullet_y[i] );
if( bullet_x[i]>mx-12 && bullet_x[i]<mx+12 ){
if( bullet_y[i]>my-12 && bullet_y[i]<my+12 ){
// 敵弾消去
sc.Hide( i+BULLET, i+BULLET );
// 自機爆発
sc.Set( 0, 5 );
play = false;
}
}
}
}
}
// 背景スクロール
scroll += 2;
if( scroll>=HEIGHT ) scroll = 0;
sc.BGScroll( 0, 0, scroll );
}
sc.Draw(); // スプライト描画
}
}
}